package Personas;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nan
 */
public class MedAltaPer implements ActionListener, KeyListener{

    private GAltaPer Ingreso = new GAltaPer();
    private Conexion.ConexionBD conectar = new Conexion.ConexionBD();
    private personaListar persona;

    public MedAltaPer(){
        Ingreso.setLocationRelativeTo(null);
        Ingreso.setActionListeners(this);
        Ingreso.setKeyListener(this);
        Ingreso.show();
    }

    MedAltaPer(personaListar aThis) {
        Ingreso.setLocationRelativeTo(null);
        Ingreso.setActionListeners(this);
        Ingreso.setKeyListener(this);
        Ingreso.show();
        this.persona = aThis;
    }

    public void setVisible(boolean mostrar){
        Ingreso.setVisible(mostrar);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == Ingreso.getjButtonAceptar()){
            String a = (Ingreso.getjTextFieldDNI().getText());
            String aa = (Ingreso.getjTextFieldNombre().getText());
            String b =(Ingreso.getjTextFieldApellido().getText());
            String c =(Ingreso.getjTextFieldDireccion().getText());
            String d =(Ingreso.getjTextFieldTelefono().getText());
            String ee =(Ingreso.getjTextFieldNombreUser().getText());
            String f =(Ingreso.getjTextFieldContra().getText());
            String g =(Ingreso.getTipoUser().getText());
            conectar.conectar();
            Statement st;
            try {
                st = conectar.getConn().createStatement();
                String insercion = "INSERT INTO \"Personas\"(\"DNI\", \"Nombre\", \"Apellido\", \"Direccion\", \"Telefono\", \"NomUsuario\", \"Contrasenia\", \"TipoUser\") VALUES ('"+a+"','"+aa+"','"+b+"','"+c+"','"+d+"','"+ee+"','"+f+"','"+g+"')";
                st.execute(insercion);
                this.persona.actualizarTabla();
                JOptionPane.showMessageDialog(null, "Se ha insertado el Articulo", "Elemento insertado", JOptionPane.INFORMATION_MESSAGE);
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Nose puede insertar", "ERROR", JOptionPane.ERROR_MESSAGE);
            }
            conectar.cerrar();
            Ingreso.dispose();
        }else{
            if(e.getSource() == Ingreso.getjButtonCancelar()){
                Ingreso.dispose();
            }
        }
    }

    public void keyTyped(KeyEvent e) {
        
    }

    public void keyPressed(KeyEvent e) {   }

    public void keyReleased(KeyEvent e) {   }

    

   
}
